package cn.droidlover.xrecyclerview.demo.slice;

import cn.droidlover.xrecyclerview.XRecyclerContentLayout;
import cn.droidlover.xrecyclerview.XRecyclerView;
import cn.droidlover.xrecyclerview.demo.ResourceTable;
import cn.droidlover.xrecyclerview.utils.LogUtils;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;

import java.util.ArrayList;
import java.util.List;

public class NextAbilitySlice extends AbilitySlice implements Component.ClickedListener {

    private XRecyclerContentLayout XRecyclerContentLayout;
    private LoadingComponent loadComponent;

    private ArrayList<String> data = new ArrayList<>();
    private AnimatorProperty mAnimator, mAnimator2;
    private Image mTopImage, mBottomImage;
    private DirectionalLayout directionalLayout;
    private int page = 1;
    private XRecyclerView listContainer;
    private final EventHandler handler = new EventHandler(EventRunner.getMainEventRunner());

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_next);
        findComponentById(ResourceTable.Id_tx_error).setClickedListener(this);
        findComponentById(ResourceTable.Id_tx_empty).setClickedListener(this);
        findComponentById(ResourceTable.Id_tx_content).setClickedListener(this);
        findComponentById(ResourceTable.Id_tx_loading).setClickedListener(this);
        XRecyclerContentLayout = (XRecyclerContentLayout) findComponentById(ResourceTable.Id_x_recycle_view);
        mTopImage = (Image) findComponentById(ResourceTable.Id_image);
        mAnimator = mTopImage.createAnimatorProperty();
        mBottomImage = (Image) findComponentById(ResourceTable.Id_image1);
        mAnimator2 = mBottomImage.createAnimatorProperty();
        directionalLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_layoutImage);
        addContentComponent();
        addLoadingComponent();
        addErrorComponent();
        addEmptyComponent();
    }


    private void startTopLoading() {

        mTopImage.setVisibility(Component.VISIBLE);
        mAnimator.setDuration(600).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).setTarget(mTopImage).start();

    }


    private void stopTopLoading() {
        mTopImage.setVisibility(Component.HIDE);
        mAnimator.cancel();
    }


    private void startBottomLoading() {
        directionalLayout.setVisibility(Component.VISIBLE);
        mAnimator2.setDuration(600).rotate(360).setLoopedCount(AnimatorProperty.INFINITE).setTarget(mBottomImage).start();
    }


    private void stopBottomLoading() {
        directionalLayout.setVisibility(Component.HIDE);
        mAnimator2.cancel();
    }

    private List<String> getData(int page) {

        if (data.size() < 60) {
            if (page == 1) {
                for (int pos = 0; pos < 12; pos++) {
                    data.add("测试" + pos);
                }
            } else {
                for (int pos = data.size(); pos < page * 12; pos++) {
                    data.add("测试" + pos);
                }
            }

        }


        return data;
    }

    //添加添加自定义的组件到EmptyView
    private void addEmptyComponent() {
        EmptyComponent emptyComponent = new EmptyComponent(this);
        XRecyclerContentLayout.addEmptyView(emptyComponent);
    }

    //添加自定义的组件到ErrorView
    private void addErrorComponent() {
        ErrorComponent errorComponent = new ErrorComponent(this);
        XRecyclerContentLayout.addErrorView(errorComponent);

    }

    //添加添加自定义的组件到LoadingView
    private void addLoadingComponent() {
        loadComponent = new LoadingComponent(this);
        XRecyclerContentLayout.addLoadingView(loadComponent);
        loadComponent.startLoading();
    }

    private void addContentComponent() {

        listContainer = new XRecyclerView(this);
        listContainer.setWidth(ComponentContainer.LayoutConfig.MATCH_PARENT);
        listContainer.setHeight(ComponentContainer.LayoutConfig.MATCH_PARENT);
        listContainer.setReboundEffect(false);
        listContainer.setLongClickable(false);
        XRecyclerContentLayout.addContentView(listContainer);
        SettingProvider provider = new SettingProvider(getData(1), this);
        listContainer.setItemProvider(provider);
        listContainer.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                data.clear();
                handler.removeTask(refreshRunnable);
                handler.postTask(refreshRunnable, 1000);
                startTopLoading();
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        data.clear();
                        page = 1;
                        getData(page);
                        provider.notifyDataChanged();
                    }
                }, 1000);
            }

            @Override
            public void onLoadMore() {
                if (data.size()>59 ) {
                    return;
                } else {
                    startBottomLoading();
                }
                handler.removeTask(refreshRunnable);
                handler.postTask(refreshRunnable, 1000);
                new EventHandler(EventRunner.getMainEventRunner()).postTask(new Runnable() {
                    @Override
                    public void run() {
                        int size=data.size();
                        page++;
                        provider.addData( getData(page));
                        provider.notifyDataSetItemRangeChanged(size,data.size());
                        provider.notifyDataChanged();

                    }
                }, 1000);
            }
        });

    }

    @Override
    public void onActive() {
        super.onActive();

    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    protected void onBackPressed() {
        super.onBackPressed();
        if (loadComponent != null) {
            loadComponent.stopLoading();
        }
        LogUtils.info("onBackPressed", "onBackPressed");
        XRecyclerContentLayout.showContent();
        XRecyclerContentLayout.removeContentView(listContainer);
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_tx_error:
                XRecyclerContentLayout.showErrorView();

                break;
            case ResourceTable.Id_tx_empty:
                XRecyclerContentLayout.showEmptyView();

                break;
            case ResourceTable.Id_tx_content:
                XRecyclerContentLayout.showContent();

                break;
            case ResourceTable.Id_tx_loading:
                XRecyclerContentLayout.showLoadingView();

                break;

        }
    }

    private final Runnable refreshRunnable = () -> {
        stopTopLoading();
        stopBottomLoading();
    };


}
